using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Collections.Generic;
using System.Text;
using SMS.PartnerUtils.Tip;
using SMS.PartnerUtils.Keo;

namespace SMS.PartnerUtils
{
    public class MOProcess
    {
        //Field
        private static Dictionary<string, string> _servDic;

        //Method
        static MOProcess()
        {
            _servDic = new Dictionary<string, string>();
            _servDic.Add("8078", "80");
            _servDic.Add("8178", "81");
            _servDic.Add("8278", "82");
            _servDic.Add("8378", "83");
            _servDic.Add("8478", "84");
            _servDic.Add("8578", "85");
            _servDic.Add("8678", "86");
            _servDic.Add("8778", "87");
        }

        public static bool ProcessMessage_ImpMO(long msgInId, int requestId, string mobileNo, string serviceId, string message, string requestTime)
        {
            // Send MO to Impact
            ImpService sendMO = new ImpService();
            int result = -1;
            bool flagIn = false;
            try
            {
                // Call receiver service
                result = sendMO.MOReceiver(requestId, mobileNo, serviceId, message.Substring(0, 2), message, sendMO.UserName, sendMO.Password);

                if (result == 0) // Success
                    UpdateStatus(msgInId, serviceId, 9);
                
                else if (result == 1) // Data input error/internet timeout
                    UpdateStatus(msgInId, serviceId, 8);
                
                else if (result == 2) // Wrong username/password             
                    UpdateStatus(msgInId, serviceId, 8);
                
                else if (result == 3) // System error
                    UpdateStatus(msgInId, serviceId, 8);
                
                else // Default
                    UpdateStatus(msgInId, serviceId, 7);
                
                flagIn = true;
            }
            catch
            {
                flagIn = false;
                UpdateStatus(msgInId, serviceId, 7);
            }
            return flagIn;
        }

        public static bool ProcessMessage_KeoLiveMO(long msgInId, string mobileNo, string serviceId, string requestId, string message, string requestTime)
        {
            // Send MO to KeoLive
            KeoLiveService sendMO = new KeoLiveService();
            SMS.PartnerUtils.Keo.AuthHeader aut = new SMS.PartnerUtils.Keo.AuthHeader();
            aut.Username = ConfigurationManager.AppSettings["KLUid"].ToString();
            aut.Password = ConfigurationManager.AppSettings["KLPwd"].ToString();
            sendMO.AuthHeaderValue = aut;

            int result = -1;
            bool flagIn = false;
            try
            {
                // Call receiver service
                result = sendMO.messageReceiver(mobileNo, serviceId, message.Substring(0, 2), message, requestId);

                if (result == 0) // Success
                    UpdateStatus(msgInId, serviceId, 9);

                else if (result == 1) // Data input error/internet timeout
                    UpdateStatus(msgInId, serviceId, 8);

                else if (result == 2) // Wrong username/password
                    UpdateStatus(msgInId, serviceId, 8);

                else if (result == 3) // System error
                    UpdateStatus(msgInId, serviceId, 8);

                else // Default
                    UpdateStatus(msgInId, serviceId, 7);

                flagIn = true;
            }
            catch
            {
                flagIn = false;
                UpdateStatus(msgInId, serviceId, 7);
            }
            return flagIn;
        }

        public static bool ProcessMessage_TIPMO(long msgInId, string mobileNo, string serviceId, string requestId, string message, string requestTime)
        {
            // Send MO to TIP
            TipService sendMO = new TipService();
            SMS.PartnerUtils.Tip.AuthHeader aut = new SMS.PartnerUtils.Tip.AuthHeader();
            aut.Username = ConfigurationManager.AppSettings["TIPUid"].ToString();
            aut.Password = ConfigurationManager.AppSettings["TIPPwd"].ToString();
            sendMO.AuthHeaderValue = aut;

            int result = -1;
            bool flagIn = false;
            try
            {
                // Call receiver service
                result = sendMO.messageReceiver(mobileNo, serviceId, message.Substring(0, 3), message, requestId);

                if (result == 0) // Success
                    UpdateStatus(msgInId, serviceId, 9);

                else if (result == 1) // Data input error/internet timeout
                    UpdateStatus(msgInId, serviceId, 8);

                else if (result == 2) // Wrong username/password
                    UpdateStatus(msgInId, serviceId, 8);

                else if (result == 3) // System error
                    UpdateStatus(msgInId, serviceId, 8);

                else // Default
                    UpdateStatus(msgInId, serviceId, 7);

                flagIn = true;
            }
            catch
            {
                flagIn = false;
                UpdateStatus(msgInId, serviceId, 7);
            }
            return flagIn;
        }

        public static bool ProcessMessage_TTGMO(long msgInId, string mobileNo, string serviceId, string requestId, string message, string requestTime)
        {
            // Send MO to ThongThuong
            Thongthuong.thongthuongsms sendMO = new Thongthuong.thongthuongsms();
            int result = -1;
            bool flagIn = false;
            try
            {
                // Call receiver service
                result = sendMO.Callthongthuongsms(ConfigurationManager.AppSettings["TTGUid"].ToString(), ConfigurationManager.AppSettings["TTGPwd"].ToString(), "thanhvx", mobileNo, serviceId, requestId, message);

                if (result == 0) // Success
                    UpdateStatus(msgInId, serviceId, 9);

                else // Default
                    UpdateStatus(msgInId, serviceId, 8);

                flagIn = true;
            }
            catch
            {
                flagIn = false;
                UpdateStatus(msgInId, serviceId, 7);
            }
            return flagIn;
        }

        private static void UpdateStatus(long msgInId, string serviceId, int status)
        {
            string connStr = ConfigurationManager.ConnectionStrings["SMSC"].ToString();

            string cmdText = ("UPDATE MsgIn_" + _servDic[serviceId] + " SET Status=@Status ") + " WHERE MsgInID=@MsgInID";
            SqlConnection cnn = new SqlConnection(connStr);
            SqlCommand cmd = new SqlCommand(cmdText, cnn);
            cmd.Parameters.Clear();

            cmd.Parameters.Add("@MsgInID", SqlDbType.BigInt).Value = msgInId;
            cmd.Parameters.Add("@Status", SqlDbType.TinyInt).Value = status;            

            cnn.Open();
            cmd.ExecuteNonQuery();
            cnn.Close();
        }
    }
}
